package com.zhijian.warehouse.manage.service.impl;

import cn.hutool.core.collection.CollectionUtil;

import com.zhijian.util.BeanUtil;
import com.zhijian.util.page.Page;
import com.zhijian.warehouse.manage.dao.StoreStockRecordProductMapper;
import com.zhijian.warehouse.manage.entity.po.StoreStockRecordProduct;
import com.zhijian.warehouse.manage.entity.vo.request.StoreStockRecordProductSaveOrUpdateVo;
import com.zhijian.warehouse.manage.entity.vo.request.StoreStockRecordProductSearchVo;
import com.zhijian.warehouse.manage.entity.vo.response.StoreStockRecordProductVo;
import com.zhijian.warehouse.manage.service.StoreStockRecordProductService;
import com.zhijian.warehouse.manage.util.AssertCommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 盘点产品明细表业务处理层
 *
 * @author auto
 * @version: 1.0
 * @date 2024-10-27 13:58:00
 */
@Slf4j
@Service
public class StoreStockRecordProductServiceImpl implements StoreStockRecordProductService {

    @Autowired
    private StoreStockRecordProductMapper mapper;

    @Override
    public StoreStockRecordProduct selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public StoreStockRecordProductVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(StoreStockRecordProduct storeStockRecordProduct) {
        Date now = new Date();
        storeStockRecordProduct.setCreateTime(now);
        storeStockRecordProduct.setUpdateTime(now);
        return mapper.insert(storeStockRecordProduct) > 0 ? storeStockRecordProduct.getId() : 0L;
    }

    @Override
    public Long insertSelective(StoreStockRecordProduct storeStockRecordProduct) {
        Date now = new Date();
        storeStockRecordProduct.setCreateTime(now);
        storeStockRecordProduct.setUpdateTime(now);
        return mapper.insertSelective(storeStockRecordProduct) > 0 ? storeStockRecordProduct.getId() : 0L;
    }

    @Override
    public boolean update(StoreStockRecordProduct storeStockRecordProduct) {
        Date now = new Date();
        storeStockRecordProduct.setUpdateTime(now);
        return mapper.updateByPrimaryKey(storeStockRecordProduct) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(StoreStockRecordProduct record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(StoreStockRecordProductSaveOrUpdateVo saveOrUpdateVo) {
        StoreStockRecordProduct storeStockRecordProduct = new StoreStockRecordProduct();
        BeanUtils.copyProperties(saveOrUpdateVo, storeStockRecordProduct);
        return this.insert(storeStockRecordProduct) != 0;
    }

    @Override
    public boolean saveByVo(StoreStockRecordProductVo saveOrUpdateVo) {
        saveOrUpdateVo.setId(null);
        StoreStockRecordProduct storeStockRecordProduct = new StoreStockRecordProduct();
        BeanUtils.copyProperties(saveOrUpdateVo, storeStockRecordProduct);
        return this.insert(storeStockRecordProduct) != 0;
    }

    @Override
    public boolean modify(StoreStockRecordProductSaveOrUpdateVo saveOrUpdateVo) {
        StoreStockRecordProduct old = this.selectById(saveOrUpdateVo.getId());
        AssertCommonUtil.notNull(old, "此数据不存在或者已被删除");
        StoreStockRecordProduct storeStockRecordProduct = new StoreStockRecordProduct();
        BeanUtils.copyProperties(saveOrUpdateVo, storeStockRecordProduct);
        return this.update(storeStockRecordProduct);
    }

    @Override
    public StoreStockRecordProductVo selectOne(StoreStockRecordProductSearchVo storeStockRecordProductSearchVo) {
        return mapper.selectOne(storeStockRecordProductSearchVo);
    }

    @Override
    public Integer count(StoreStockRecordProductSearchVo storeStockRecordProductSearchVo) {
        return mapper.count(storeStockRecordProductSearchVo);
    }

    @Override
    public List<StoreStockRecordProductVo> selectList(StoreStockRecordProductSearchVo storeStockRecordProductSearchVo) {
        return mapper.selectList(storeStockRecordProductSearchVo);
    }

    @Override
    public Page<StoreStockRecordProductVo> selectPage(StoreStockRecordProductSearchVo storeStockRecordProductSearchVo) {
        AssertCommonUtil.check(Objects.isNull(storeStockRecordProductSearchVo.getPageNum()) || Objects.isNull(storeStockRecordProductSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(storeStockRecordProductSearchVo);
        //分页
        Page<StoreStockRecordProductVo> page = new Page<>(true, count, storeStockRecordProductSearchVo.getPageNum(), storeStockRecordProductSearchVo.getPageSize());
        storeStockRecordProductSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<StoreStockRecordProductVo> responseVos = this.selectList(storeStockRecordProductSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<StoreStockRecordProduct> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(StoreStockRecordProductSearchVo storeStockRecordProductSearchVo) {
        if (BeanUtil.isEmpty(storeStockRecordProductSearchVo.getStoreStockRecordId()))
            return;
        //慎重用此方法！！
        List<StoreStockRecordProductVo> storeStockRecordProductVos = selectList(storeStockRecordProductSearchVo);
        if (BeanUtil.isEmpty(storeStockRecordProductVos))
            return;
        //通过流获取所有id
        List<Long> idList = storeStockRecordProductVos.stream().map(StoreStockRecordProductVo::getId).collect(Collectors.toList());
        //根据id删除
        deleteByIdList(idList);
    }
}